package source.view;

import java.util.Hashtable;
import java.io.*;


/**
 * The DisplayLookupTable is used by the AreaViewport to look up the
 * Filepath or display name associated with a keyword given by the model.
 * It loads its data from a supplied data file, but if the data is modifed
 * during execution, it will not be saved to file in this version.
 * 
 * @author Brent
 * @version 1.0
 *
 */
public class DisplayLookupTable {

	protected Hashtable<String, String> m_hash;
	
	public DisplayLookupTable(String sourceFile) 
	{
		
		m_hash = new Hashtable<String, String>();
		
		// load from file
		try
		{
			FileInputStream inFile = new FileInputStream(sourceFile);
			BufferedReader inBuff = new BufferedReader(new InputStreamReader(inFile));
			
			String current;
			String[] parts;
			
			while((current = inBuff.readLine()) != null)
			{
				parts = current.split(":");
				
				m_hash.put(parts[0], parts[1]);
			}
		} catch (FileNotFoundException e)
		{
			MainScreen.writeToConsole("DisplayLookupTable: ERROR: " + e.toString());
			MainScreen.writeToConsole("DisplayLookupTable: Printing Stack Trace:");
			MainScreen.writeToConsole("DisplayLookupTable: " + e.getStackTrace().toString());
		} catch (IOException e)
		{
			MainScreen.writeToConsole("DisplayLookupTable: ERROR: " + e.toString());
			MainScreen.writeToConsole("DisplayLookupTable: Printing Stack Trace:");
			MainScreen.writeToConsole("DisplayLookupTable: " + e.getStackTrace().toString());
		} catch (IndexOutOfBoundsException e)
		{
			MainScreen.writeToConsole("DisplayLookupTable: ERROR: " + e.toString());
			MainScreen.writeToConsole("DisplayLookupTable: Source file " + sourceFile + " may be malformed.");
			MainScreen.writeToConsole("DisplayLookupTable: Printing Stack Trace:");
			MainScreen.writeToConsole("DisplayLookupTable: " + e.getStackTrace().toString());			
		} catch (NullPointerException e)
		{
			MainScreen.writeToConsole("DisplayLookupTable: ERROR: " + e.toString());
			MainScreen.writeToConsole("DisplayLookupTable: Source file " + sourceFile + " may be malformed.");
			MainScreen.writeToConsole("DisplayLookupTable: Printing Stack Trace:");
			MainScreen.writeToConsole("DisplayLookupTable: " + e.getStackTrace().toString());			
		}	
	}
	
	public String lookup(String key)
	{
		return m_hash.get(key);
	}
}
